package hhpoll.domain;

import java.io.Serializable;

import org.hibernate.Session;

import hhpoll.util.HibernateUtil;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;


public class User {
	private Long id;
    private String email;
    private String password;
    private String firstName;
    private String lastName;
    private String alias;
    private java.util.Date createDate;
    
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}
	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}
	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}
	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	/**
	 * @return the alias
	 */
	public String getAlias() {
		return alias;
	}
	/**
	 * @param alias the alias to set
	 */
	public void setAlias(String alias) {
		this.alias = alias;
	}
	/**
	 * @return the createDate
	 */
	public java.util.Date getCreateDate() {
		return createDate;
	}
	/**
	 * @param createDate the createDate to set
	 */
	public void setCreateDate(java.util.Date createDate) {
		this.createDate = createDate;
	}
	
	public void save() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		// TODO: add validation logic
		session.save(this);
	}
	
	public User get(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		// TODO: add validation logic
		return (User)session.get(User.class, id);
	}
	
	public User findByEmail(String email) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		return (User)session.createQuery("from User where email = :email")
		                    .setString("email", email)
		                    .uniqueResult();
	}

    public void fromXML (Element element) throws NumberFormatException {
        // first, normalize to ensure there are no adjacent text nodes
        element.normalize();

        // Now walk through the children and set attributes
        NodeList nodeList = element.getChildNodes();
        for (int i=0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Node child = node.getFirstChild();
                if (child == null) continue;

                if ("id".equals(node.getLocalName())) {
                    setId(Long.valueOf(child.getNodeValue()));
                } else if ("firstName".equals(node.getLocalName())) {
                    setFirstName(child.getNodeValue());
                } else if ("lastName".equals(node.getLocalName())) {
                    setLastName(child.getNodeValue());
                } else if ("email".equals(node.getLocalName())) {
                    setEmail(child.getNodeValue());
                } else if ("password".equals(node.getLocalName())) {
                    setPassword(child.getNodeValue());
                } else if ("alias".equals(node.getLocalName())) {
                    setAlias(child.getNodeValue());
                }
            }
        }
    }
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		return true;
	}

}
